package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.article.mapper.TravelMapper;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * @author 猫教授
 * @date 2019/8/11 23:04
 */
@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper travelContentMapper;
    @Override
    public PageInfo<Travel> selectForList(TravelQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrder());
        List<Travel> list =travelMapper.selectForList(qo);
        return new PageInfo<Travel>(list);
    }

    @Override
    public Travel saveOrUpdate(Travel travel, UserInfo userInfo) {
        //先对所有没有的数据进行处理
        //作者
        travel.setAuthor(userInfo);
        //摘要
        String content = travel.getTravelContent().getContent();
        if (content.length()>100){
            travel.setSummary(content.substring(0,100));
        }else {
            travel.setSummary(content);
        }
        //最后修改时间
        travel.setLastUpdateTime(new Date());

        if (travel.getId()==null){
            //创建时间
            travel.setCreateTime(new Date());
            travelMapper.insert(travel);
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());
            travelContentMapper.insert(travelContent);
            return travel;
        }else {
            travelMapper.updateByPrimaryKey(travel);
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());
            travelContentMapper.updateTravelContentById(travelContent);
            return travel;
        }
    }

    @Override
    public Travel selectTravelById(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public TravelContent getContent(Long travelId) {
        return travelContentMapper.getContentByTravelId(travelId);
    }

    @Override
    public List<Travel> selectTravelsByDestIdNumTop3(Long destId) {

        return travelMapper.selectTravelsByDestIdNumTop3(destId);
    }

    public PageInfo<Travel> getList(TravelQuery qo) {
        //因为游记列表需要进行排序具体以什么时间排序,暂时还W为确定,所以先空字符站位
        //TODO 游记列表排序暂时还没确定,这里的排序规则应该也算是高级查询的一种,所以也可以算是条件对象的一个字段

        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.selectAll(qo);
        return new PageInfo<Travel>(list);
    }

    @Override
    public void updateState(Long id, Integer state) {
        travelMapper.updateState(id,state);
    }

    @Override
    public int addReplynumByTravelId(Long travelId) {
        travelMapper.addReplynumByTravelId(travelId);
        //然后把最新数量返回
        return travelMapper.selectReplynumById(travelId);
    }

    @Override
    public Travel get(Long id) {

        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Travel> selectAll() {

        return travelMapper.listAll();
    }

    @Override
    public PageInfo<Travel> selectList(TravelQuery qo) {


        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrder());
        List<Travel> list = travelMapper.listAll();
        return new PageInfo<Travel>(list);
    }

    @Override
    public List<Travel> getTravel(Long id) {
        return travelMapper.selectTravels(id);
    }

    @Override
    public PageInfo query(TravelQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());

        return new PageInfo(travelMapper.selectForList2(qo));
    }

    @Override
    public int getCount(Long id) {
        return travelMapper.selectTravelCountByuserId(id);
    }


}
